
package org.zju.demo.shoppingdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.*;

import com.google.gson.Gson;

import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.adapter.CartItemListAdapter;
import org.zju.demo.shoppingdemo.application.ShoppingApplication;
import org.zju.demo.shoppingdemo.controller.ShoppingController;
import org.zju.demo.shoppingdemo.model.Address;
import org.zju.demo.shoppingdemo.model.Product;
import org.zju.demo.shoppingdemo.model.Request.BuyProductsModel;
import org.zju.demo.shoppingdemo.model.Request.RequestProduct;
import org.zju.demo.shoppingdemo.model.response.BaseResponse;
import org.zju.demo.shoppingdemo.utils.Constant;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Created by ZJDX on 2015/9/13.
 */

public class CartActivity extends CommonActivity {

    private static final int REQUEST_LOGIN = 100;

    private FrameLayout notEmptyFrame;
    private ListView shopCarListView;
    private TextView totalView;
    private FrameLayout emptyFrame;
    private TextView shopCarSubmit;
    private List<Product> productList;
    private String username;
    private CartItemListAdapter adapter;
    private ImageView backToHome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 不显示标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cart);
        totalView= (TextView)findViewById(R.id.shop_car_footer_total);
        productList = ((ShoppingApplication)getApplication()).getShoppingCart();
        username = ((ShoppingApplication)getApplication()).getUsername();
        backToHome= (ImageView)findViewById(R.id.top_view_back);
        notEmptyFrame = (FrameLayout)findViewById(R.id.shop_car_isnot);
        shopCarListView = (ListView)findViewById(R.id.shop_car_list);
        emptyFrame = (FrameLayout)findViewById(R.id.shop_car_null);
        shopCarSubmit = (TextView)findViewById(R.id.shop_car_submit);
        initBottomMenu();
        Listener listener = new Listener();
        shopCarSubmit.setOnClickListener(listener);
        emptyFrame.setOnClickListener(listener);
        backToHome.setOnClickListener(listener);
        adapter = new CartItemListAdapter(this, productList);
        shopCarListView.setAdapter(adapter);
        if (productList.size() == 0) {
            notEmptyFrame.setVisibility(View.GONE);
            emptyFrame.setVisibility(View.VISIBLE);
        } else {
            notEmptyFrame.setVisibility(View.VISIBLE);
            emptyFrame.setVisibility(View.GONE);
        }
        Double total=0.0;
        for(int i=0;i<productList.size();i++)
            total=total+productList.get(i).getPrice();
        totalView.setText(Double.toString(total));
    }

    private class Listener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.shop_car_submit:
                    Toast.makeText(CartActivity.this, "购买成功！", Toast.LENGTH_SHORT).show();
                    Iterator<Product> it = productList.iterator();
                    while (it.hasNext()) {
                        Product value = it.next();
                        it.remove();
                    }
                    shopCarListView.setAdapter(null);
                    totalView.setText(Double.toString(0.0));
                    /*username = ((ShoppingApplication)getApplication()).getUsername();
                    if(username == null) {
                        Toast.makeText(CartActivity.this, "请先登录", Toast.LENGTH_SHORT);
                        Intent intent= new Intent();
                        intent.setClass(CartActivity.this, LoginActivity.class);
                        startActivityForResult(intent, REQUEST_LOGIN);
                    } else {
                        Log.e("CartActivity", "Submit");
                        BuyProductsModel model = new BuyProductsModel();
                        model.setUsername(username);
                        model.setCommandId(Constant.COMMAND_BUY);
                        //Get default address
                        Address.findWithQuery(Address.class, "Select * from Address where username = ?", username);
                        List<Address> address = Address.findWithQuery(Address.class, "Select * from Address where username = ? and default_address = 1", username);
                        if (address == null) {
                            address = Address.findWithQuery(Address.class, "Select * from Address where username = ? and default_address = 0", username);
                        }
                        model.setAddress(address.get(0).getAddress());
                        List<RequestProduct> requestProducts = new ArrayList<RequestProduct>();
                        for(Product product: productList) {
                            RequestProduct requestProduct = new RequestProduct();
                            requestProduct.setNumber(product.getNumber());
                            requestProduct.setProductId(product.getProductId());
                            requestProducts.add(requestProduct);
                        }
                        model.setProducts(requestProducts);
                        Log.e("CartActivity", new Gson().toJson(model));
                        ShoppingController.doBuyProduct(CartActivity.this, model);
                    }*/
                    break;
                case R.id.shop_car_null:
                    Intent intent = new Intent();
                    intent.setClass(CartActivity.this, WelcomeActivity.class);
                    startActivity(intent);
                case R.id.top_view_back:
                    Intent intentback = new Intent();
                    intentback.setClass(CartActivity.this, WelcomeActivity.class);
                    startActivity(intentback);

                default:
            }
        }
    }

    @Override
    public void onResponseMessage(BaseResponse response) {
        super.onResponseMessage(response);
        if (response.getResult() == 0) {
            Toast.makeText(this, "购物成功", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void initBottomMenu() {
        Button transBtn;
        transBtn=(Button)findViewById(R.id.transButton);
        transBtn.getBackground().setAlpha(200);//更改半透明度
        imageViewIndex = (ImageView) findViewById(R.id.menu_home_img);
        imageViewIndex.setOnTouchListener(indexListener);
        imageViewIndex.setImageResource(R.drawable.menu_home_released);

        imageViewShopping = (ImageView) findViewById(R.id.menu_shopping_cart_img);
//        imageViewShopping.setOnTouchListener(shoppingListener);
        imageViewShopping.setImageResource(R.drawable.menu_shopping_cart_pressed);

        imageViewMyInfo = (ImageView) findViewById(R.id.menu_personal_center_image);
        imageViewMyInfo.setOnTouchListener(myInfoListener);
        imageViewMyInfo.setImageResource(R.drawable.menu_my_info_released);
    }

}
